package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.UserAddresses;
import com.example.mall.dto.UserAddressesDto;
import com.example.mall.mapper.UserAddressesMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserAddressesQuery;
import com.example.mall.service.IUserAddressesService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户地址表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class UserAddressesServiceImpl extends ServiceImpl<UserAddressesMapper, UserAddresses> implements IUserAddressesService {

    @Autowired
    private UserAddressesMapper userAddressesMapper;

    @Override
    public PageDTO<UserAddressesDto> queryPage(UserAddressesQuery userAddressesQuery) {
        if (userAddressesQuery == null) {
            userAddressesQuery = new UserAddressesQuery();
        }
        Page<UserAddresses> page = userAddressesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<UserAddresses> cartPage = lambdaQuery()
                .like(userAddressesQuery.getAddressId()!= null, UserAddresses::getAddressId, userAddressesQuery.getAddressId())
                .like(userAddressesQuery.getUserId()!= null, UserAddresses::getUserId, userAddressesQuery.getUserId())
                .like(userAddressesQuery.getRecipientPhone()!= null, UserAddresses::getRecipientPhone, userAddressesQuery.getRecipientPhone())
                .like(userAddressesQuery.getAddress()!= null, UserAddresses::getAddress, userAddressesQuery.getAddress())
                .like(userAddressesQuery.getCity()!= null, UserAddresses::getCity, userAddressesQuery.getCity())
                .like(userAddressesQuery.getState()!= null, UserAddresses::getState, userAddressesQuery.getState())
                .like(userAddressesQuery.getPostalCode()!= null, UserAddresses::getPostalCode, userAddressesQuery.getPostalCode())
                .like(userAddressesQuery.getIsDefault()!= null, UserAddresses::getIsDefault, userAddressesQuery.getIsDefault())
                .like(userAddressesQuery.getStatus()!= null, UserAddresses::getStatus, userAddressesQuery.getStatus())
                .orderByDesc(UserAddresses::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, UserAddressesDto.class);
    }

    @Override
    public UserAddresses queryUserAddressesById(Long addressId) {
        AssertUtil.isNotNull(addressId, "addressId不能为空");
        return userAddressesMapper.selectById(addressId);
    }

    @Override
    public List<UserAddresses> queryUserAddressesList() {
        return this.list();
    }

    @Override
    public void insert(UserAddressesDto userAddressesDto) {
        UserAddresses userAddresses = new UserAddresses();
        CopyUtils.copyProperties(userAddressesDto, userAddresses);
        userAddresses.setAddressId(CodeGenerateUtils.generateUniqueId());
        userAddresses.setCreateDate(new Date());
        userAddresses.setUpdateDate(new Date());
        userAddresses.setStatus(BaseConstants.EFFECTIVE_STATUS);
        userAddressesMapper.insert(userAddresses);
    }

    @Override
    public void update(UserAddressesDto userAddressesDto) {
        AssertUtil.isNotNull(userAddressesDto.getAddressId(), "addressId不能为空");
        UserAddresses userAddresses = userAddressesMapper.selectById(userAddressesDto.getAddressId());
        AssertUtil.isNotNull(userAddresses, "address不存在");
        CopyUtils.copyProperties(userAddressesDto, userAddresses);
        userAddresses.setUpdateDate(new Date());
        userAddressesMapper.updateById(userAddresses);
    }

    @Override
    public void delete(Long addressId) {
        AssertUtil.isNotNull(addressId, "addressId不能为空");
        UserAddresses userAddresses = userAddressesMapper.selectById(addressId);
        AssertUtil.isNotNull(userAddresses, "address不存在");
        userAddresses.setStatus(BaseConstants.LOSE_STATUS);
        userAddresses.setUpdateDate(new Date());
        userAddressesMapper.updateById(userAddresses);
    }

    @Override
    public void deleteBatch(List<Long> addressIds) {
        AssertUtil.isNotNull(addressIds, "addressIds不能为空");
        List<UserAddresses> userAddresses = userAddressesMapper.selectBatchIds(addressIds);
        AssertUtil.isNotNull(userAddresses, "address不存在");
        for (UserAddresses userAddress : userAddresses) {
            userAddress.setStatus(BaseConstants.LOSE_STATUS);
            userAddress.setUpdateDate(new Date());
            userAddressesMapper.updateById(userAddress);
        }
    }
}
